package de.imdb250junkie.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ResourceBundle;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.LineBorder;

import de.imdb250junkie.locale.LocaleUtils;
import de.imdb250junkie.utils.Movie;

/**
 * @author André Stannek
 * @version 2010-02-10
 */
public class ListElement extends JPanel implements ListCellRenderer<Movie>
{
    /**
     * 
     */
    private static final long serialVersionUID = -7536759073327083903L;

    /**
     * 
     */
    private final transient JLabel locTitleLabel, rankLabel, ratingLabel,
            titleLabel, yearLabel;

    /**
     * 
     */
    private transient Movie movie;

    /**
     * Default contructor.
     */
    public ListElement()
    {
        super();
        titleLabel = new JLabel();
        titleLabel.setHorizontalAlignment(JLabel.LEFT);
        locTitleLabel = new JLabel();
        rankLabel = new JLabel();
        yearLabel = new JLabel();
        ratingLabel = new JLabel();

        final ResourceBundle locale = ResourceBundle.getBundle(
                "de.imdb250junkie.locale.GUIStrings", LocaleUtils.getLocale());

        this.setLayout(new GridBagLayout());

        final GridBagConstraints constraint = new GridBagConstraints();

        constraint.fill = GridBagConstraints.HORIZONTAL;

        constraint.anchor = GridBagConstraints.LINE_START;

        final double leftColumWidth = 0.2;
        final double rigthColumWidth = 1 - leftColumWidth;

        constraint.gridx = 0;
        constraint.gridy = 0;
        constraint.gridwidth = 2;
        constraint.weightx = leftColumWidth;
        this.add(new JLabel(locale.getString("rank")), constraint);
        constraint.gridx = 2;
        constraint.gridy = 0;
        constraint.weightx = rigthColumWidth;
        this.add(rankLabel, constraint);
        constraint.gridx = 0;
        constraint.gridy = 1;
        constraint.weightx = leftColumWidth;
        this.add(new JLabel(locale.getString("title")), constraint);
        constraint.gridx = 2;
        constraint.gridy = 1;
        constraint.weightx = rigthColumWidth;
        this.add(titleLabel, constraint);
        constraint.gridx = 0;
        constraint.gridy = 2;
        constraint.weightx = leftColumWidth;
        this.add(new JLabel(locale.getString("loc_title")), constraint);
        constraint.gridx = 2;
        constraint.gridy = 2;
        constraint.weightx = rigthColumWidth;
        this.add(locTitleLabel, constraint);
        constraint.gridx = 0;
        constraint.gridy = 3;
        constraint.weightx = leftColumWidth;
        this.add(new JLabel(locale.getString("year")), constraint);
        constraint.gridx = 2;
        constraint.gridy = 3;
        constraint.weightx = rigthColumWidth;
        this.add(yearLabel, constraint);
        constraint.gridx = 0;
        constraint.gridy = 4;
        constraint.weightx = leftColumWidth;
        this.add(new JLabel(locale.getString("rating")), constraint);
        constraint.gridx = 2;
        constraint.gridy = 4;
        constraint.weightx = rigthColumWidth;
        this.add(ratingLabel, constraint);

        final Dimension windowSize = new Dimension(400, 87);

        this.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
        this.setSize(windowSize);
        this.setMaximumSize(windowSize);
        this.setMinimumSize(windowSize);
        this.setPreferredSize(windowSize);
        this.setVisible(true);

    }

    /**
     * update the UI Components.
     */
    private void update()
    {
        titleLabel.setText(movie.getOriginalTitle());
        locTitleLabel.setText(movie.getLocalizedTitle());
        rankLabel.setText(String.valueOf(movie.getRank()));
        yearLabel.setText(String.valueOf(movie.getYear()));
        ratingLabel.setText(String.valueOf(movie.getRating()));
    }

    @Override
    public final Component getListCellRendererComponent(
            final JList<? extends Movie> list, final Movie value,
            final int index, final boolean isSelected,
            final boolean cellHasFocus)
    {
        final Color boughtColor = new Color(30, 100, 30);
        final Color seenColor = new Color(60, 200, 60);
        final Color unseenColor = new Color(200, 60, 65);
        final int selectionColorMultiplicator = 55;

        movie = value;
        update();

        switch (movie.getState())
        {
        case SEEN:
            setBackground(seenColor);
            break;
        case BOUGHT:
            setBackground(boughtColor);
            break;
        default:
            setBackground(unseenColor);
            break;
        }
        if (isSelected)
        {
            setBackground(multiplyColor(getBackground(),
                    selectionColorMultiplicator));
        }
        else
        {
            setBackground(multiplyColor(getBackground(), 0));
        }

        return this;
    }

    /**
     * Multiplies a Color by a factor, making it brighter.
     * 
     * @param color
     *            the color
     * @param factor
     *            the factor
     * @return the new Color
     */
    private static Color multiplyColor(final Color color, final int factor)
    {
        return new Color((color.getRed() + factor),
                (color.getGreen() + factor), (color.getBlue() + factor));
    }

}
